package com.liujun.security.controller;

import com.liujun.security.entity.SysUser;
import com.liujun.security.response.Result;
import com.liujun.security.response.ResultCode;
import com.liujun.security.server.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @author pc
 */
@RestController
@RequestMapping(value = "/user")
@Api(tags = "用户接口")
public class UserController {


    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation(value = "保存用户")
    @PostMapping(value = "/save")
    public Result save(@RequestBody SysUser sysUser) {

        Boolean booleanResult = userService.save(sysUser);

        if (booleanResult) {
            return Result.SUCCESS();
        } else {
            return Result.ERROR();
        }
    }


    @ApiOperation(value = "获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", paramType = "path", dataTypeClass = Integer.class, defaultValue = "1"),
    })
    @PostMapping(value = "/getUserById/{id}")
    public Result getUserById(@PathVariable int id) {

        SysUser sysUser = userService.getUserById(id);

        if (sysUser != null) {
            return new Result(ResultCode.SUCCESS, sysUser);
        } else {
            return Result.ERROR();
        }
    }
}
